package com.lincoln.jenkins.test;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class MsgModel {
    private String robot;
    private EventEnum event;
    private String content;
    private String fromId;
    private String toId;
    private String type;

    private Map<String, String> params;

    public MsgModel(String a) {
        if (null == a) {
            a = "";
        }
        a = a.trim();
        if (!a.equals("")) {
            //有参数
            String[] p = a.split("&");
            params = new HashMap<>();
            for (String param : p) {
                String[] keyValue = param.split("=");
                try {
                    params.put(keyValue[0], keyValue.length > 1 ? URLDecoder.decode(keyValue[1], "UTF-8"): "");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            this.robot = params.get("Robot");
            this.event = EventEnum.findByType(params.get("Event"));
            this.content = params.get("content");
            this.fromId = params.get("from_id");
            this.toId = params.get("to_id");
            this.type = params.get("Type");
        }
    }
}
